package com.source.lightfowl.barcodeMachineRepair.service;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.source.lightfowl.VO.MachineRepairVO;
import com.source.lightfowl.VO.SalesTerminalVO;
import com.source.lightfowl.barcodeMachineRepair.dao.MachineRepairDao;
import com.source.lightfowl.barcodeMachineRepair.pojo.MachineRepair;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;



/**
 * 服务层
 *
 * @author Administrator
 *
 */
@Service
@SuppressWarnings("all")
public class MachineRepairService {

	@Autowired
	private MachineRepairDao machineRepairDao;
	@Autowired
	private PersonDao personDao;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<MachineRepairVO> findAll() {
		List<MachineRepair> list = machineRepairDao.findAll();
		List<MachineRepairVO> collect = list.stream().map(s -> {
			MachineRepairVO vo = new MachineRepairVO();
			BeanUtils.copyProperties(s, vo);

//			Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getRepairPerson()).longValue()));
			Person user = personDao.findVO(s.getRepairPerson());
			if(user != null){
				vo.setRepairName(user.getLoginName());

			}

			Person vo1 = personDao.findVO(s.getCreateBy());
			if (personDao != null){
				vo.setCreateName(vo1.getName());
			}
			//	private String createByName;//创建者姓名


			return vo;
		}).collect(Collectors.toList());

		return collect;
	}


	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public MachineRepairVO findById(BigInteger id) {
		Optional<MachineRepair> optional = machineRepairDao.findById(id);
		if (optional.isPresent()){
			MachineRepair s = optional.get();
			MachineRepairVO vo = new MachineRepairVO();
			BeanUtils.copyProperties(s, vo);


			Person user = personDao.findVO(s.getRepairPerson());
			if(user != null){
				vo.setRepairName(user.getLoginName());

			}

			Person vo1 = personDao.findVO(s.getCreateBy());
			if (personDao != null){
				vo.setCreateName(vo1.getName());
			}
			return vo;
		}

		return null;
	}

	/**
	 * 增加
	 * @param machineRepair
	 */
	public void add(MachineRepair machineRepair) {
		Calendar calendar= Calendar.getInstance();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
		String tmp = dateFormat.format(calendar.getTime());
		try{
			Date data = dateFormat.parse(tmp);
			machineRepair.setCreateDate(data);
		}catch (ParseException e){
			System.out.println(e);
		}
		machineRepairDao.save(machineRepair);
	}

	/**
	 * 修改
	 * @param machineRepair
	 */
	public void update(MachineRepair machineRepair) {
		machineRepairDao.save(machineRepair);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(BigInteger id) {
		machineRepairDao.deleteById(id);
	}

	@SuppressWarnings("all")
	public List<MachineRepairVO> findAllPage(int page, int pageNum) {

		Page<MachineRepair> all = machineRepairDao.findAll(PageRequest.of(page - 1, pageNum));

		List<MachineRepairVO> collect  = all.get().map(s -> {
			MachineRepairVO vo = new MachineRepairVO();
			BeanUtils.copyProperties(s, vo);

//			Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getRepairPerson()).longValue()));
			Person user = personDao.findVO(s.getRepairPerson());
			if (user != null) {
				vo.setRepairName(user.getLoginName());
			}

			return vo;
		}).collect(Collectors.toList());

		return collect;
	}
}
